﻿/*
创建人：zhangrj
日期：2022-03-21
描述：工序工区、工时、设备、生产周期维护页面js
*/
var page = {
    pageType: getParam("type"), //type:1工区班组维护，2工时维护，3设备维护，4生产周期维护，5商品工时
    commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用方法后台路径
    mainUrl: getRootPath() + "/Ashx/WorklfowStepMaintHandler.ashx", //本页面后台路径
    //初始化页面
    init: function () {
        $('document').ready(function () {
            $("#divWorkcenter").find("span").css("margin-top", "0px");
            $("#divTime").find("span").css("margin-top", "0px");
            $("#divProductionCycle").find("span").css("margin-top", "0px");
            $("#divResource").find("span").css("margin-top", "0px");
            $("#divCommodityTime").find("span").css("margin-top", "0px");
        })
        page.bind();
        page.initData(); //初始化页面数据
    },
    //页面事件绑定
    bind: function () {
        var wname = decodeURIComponent(getParam("wname")); var wid = getParam("wid"); var pname = decodeURIComponent(getParam("pname")); var pid = getParam("pid") //add:Wangjh 20230822 
        //列表加载
        $("#gdStepInfo").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'WORKFLOWSTEPNAME', title: '工序', width: 200 },
                 { field: 'OPDESC', hidden: true },
                 { field: 'WORKCENTERNAME', title: '工区', width: 150 },
                 { field: 'WORKCENTERID', hidden: true },
                 { field: 'TEAMNAME', title: '班组', width: 120 },
                 { field: 'TEAMID', hidden: true },
                 { field: 'IsUpdateWorkcenter', hidden: true },
                 { field: 'DURATIONPERUNIT', title: '单位工时', width: 100, hidden: true },
                 { field: 'SETUPTIME', title: '准备工时', width: 100, hidden: true },
                 { field: 'IsUpdateTime', hidden: true },
                 { field: 'PREPARECYCLE', title: '准备时间(分)', width: 100, hidden: true },
                 { field: 'MAKECYCLE', title: '加工时间(分)', width: 100, hidden: true },
                 { field: 'TRANSFERCYCLE', title: '转运时间(分)', width: 100, hidden: true },
                 { field: 'SHIPCYCLE', title: '转工周期(小时)', width: 100, hidden: true },
                 { field: 'COMMICYCLE', title: '外委周期(天)', width: 80, hidden: true },
                 { field: 'MAKEEMPCOUNTS', title: '作业人数', width: 80, hidden: true },
                 { field: 'CHARGINGQTY', title: '装炉/槽量', width: 80, hidden: true },
                 { field: 'MULTIMACHINEPARALLELNAME', title: '多机并行', width: 80, hidden: true },
                 { field: 'MULTIMACHINEPARALLEL', hidden: true },
                 { field: 'NOTES', title: '备注', width: 180, hidden: true },
                 { field: 'IsUpdateCycle', hidden: true },
                 { field: 'COMMODITYTIME', title: '商品工时', width: 180, hidden: true },
                 { field: 'IsUpdateCommodityTime', hidden: true }
                ]]
        });
        //绑定件号选择组件
        $("#productContent").productCombo({
            param: { productName: pname, productID: pid },
            search: function () {
                clearEasyuiDataGrid("gdStepInfo");
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
                page.clearPage();
            },
            cancel: function () { //件号取消点击事件
                clearEasyuiDataGrid("gdStepInfo");
                page.clearPage();
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
            },
            selected: function (val, text) {//件号下拉选择事件
                clearEasyuiDataGrid("gdStepInfo");
                $("#workflowContent").workflowCombo("init"); //同时恢复初始化工艺下拉组件
                $("#workflowContent").workflowCombo("setCombo", { productName: text }); //联动工艺下拉
                $("#workflowContent").workflowCombo("setValue", wid);
                page.clearPage();
            }
        });
        //绑定工艺选择组件
        $("#workflowContent").workflowCombo({
            //            param: { isThisFactory: true },
            cancel: function () {//工艺取消点击事件
                clearEasyuiDataGrid("gdStepInfo");
                page.clearPage();
            },
            selected: function (val, text) {//工艺下拉选择事件
                //clearEasyuiDataGrid("gdStepInfo");
                page.search(val);
            }
        });
        //查询
        $("#btnSearch").on("click", function () {
            var workflowId = $("#workflowContent").workflowCombo("getValue");
            if (isNullOrWhiteSpaceStr(workflowId)) {
                displayMessage("没有选择工艺路线", false);
                return;
            }
            page.search(workflowId);
        }),
        //属性获取
        $('#btnOtherRev').on("click", function () {
            clearMessage();
            page.copyOtherRev();
        });
        //保存
        $('#btnSave').on("click", function () {
            clearMessage();
            page.save();
        });
        //实做工时传递
        $('#btnWorkTimeUp').on("click", function () {
            clearMessage();
            var datas = $("#gdStepInfo").datagrid('getChecked'); //获取选中行
            if (datas.length === 0) {
                displayMessage2("工序表没有选择数据", false, ["btnWorkTimeUp"]);
                return;
            }
            //获取选中的工序ID
            var specids = new Array();
            datas.forEach(function (item, i) {
//                specids.push(item.SPECID);
                specids.push(item.WORKFLOWSTEPID);
            })//
            var workflowId = $("#workflowContent").workflowCombo("getValue");
            if (isNullOrWhiteSpaceStr(workflowId)) {
                displayMessage("没有选择工艺路线", false);
                return;
            }
            page.WorkTimeUp(workflowId, specids);
        });
    },
    //初始化页面
    initData: function () {
        $("#divWorkcenter").hide();
        $("#divTime").hide();
        $("#divResource").hide();
        $("#divProductionCycle").hide();
        $("#divCommodityTime").hide();
        var typearr = page.pageType.split(',');
        if (page.pageVaild(typearr)) {
            for (var i = 0; i < typearr.length; i++) {
                switch (typearr[i]) {
                    case "1":
                        $("#divWorkcenter").show();
                        workCenter.init(); //初始化工区班组维护
                        break;
                    case "2":
                        $("#divTime").show();
                        stepTime.init(); //初始化工时维护
                        break;
                    case "3":
                        $("#divResource").show();
                        resource.init(); //初始化设备页面
                        page.stepGridEvent(); //绑定工序表格事件
                        break;
                    case "4":
                        $("#divProductionCycle").show();
                        productionCycle.init(); //初始化生产周期维护
                        break;
                    case "5":
                        $("#divCommodityTime").show();
                        rbTime.init(); //初始化商品工时
                        break;
                    default:
                        break;
                }
            }
        }
    },
    //查询
    search: function (workflowId) {
        page.clearPage();
        clearEasyuiDataGrid("gdStepInfo");
        var formData = {
            action: "GetStepData",
            workflowId: workflowId,
            productName: $("#productContent").productCombo("getText"),
            isSomeWorkflow: $("#isSomeWorkflow").prop('checked'),
            isThisFactory: false
        };
        $.postAjax({
            url: page.mainUrl,
            data: formData,
            ajaxSuccess: function (res) {
                if (res.Result) {
                    $("#gdStepInfo").datagrid('loadData', { rows: res.DBDataSet.dtStep });
                    if (res.DBDataSet.dtOtherRevWF.length > 0) {
                        //绑定其他工艺版本下拉框
                        $("#selWfOtherRev").combobox({
                            valueField: 'WORKFLOWID',
                            textField: 'WORKFLOWNAME',
                            panelHeight: 200,
                            data: res.DBDataSet.dtOtherRevWF, //下拉数据
                            value: res.DBDataSet.dtOtherRevWF[0].WORKFLOWID //默认第一个
                        });
                    } else {
                        $("#selWfOtherRev").combobox("clear");
                        $("#selWfOtherRev").combobox("loadData", {});
                    }
                } else {
                    displayMessage(res.Message, res.Result);
                }
            }
        });
    },
    //复制其他版本工艺
    copyOtherRev: function () {
        $('#btnOtherRev').linkbutton('disable'); //禁用按钮
        var otherRevWorkflowId = $("#selWfOtherRev").combobox("getValue");
        var otherReWorkflowName = $("#selWfOtherRev").combobox("getText");
        if (isNullOrWhiteSpaceStr(otherRevWorkflowId)) {
            displayMessage2("请选择其他工艺版本", false, ["btnOtherRev"]);
            return;
        }
        var workflowId = $("#workflowContent").workflowCombo("getValue");
        if (isNullOrWhiteSpaceStr(workflowId)) {
            displayMessage2("未选择工艺", false, ["btnOtherRev"]);
            return;
        }
        var datas = $("#gdStepInfo").datagrid('getRows'); //获取所有行
        if (datas.length === 0) {
            displayMessage2("工序表没有数据", false, ["btnOtherRev"]);
            return;
        }
        $.messager.confirm('属性获取确认', '是否获取' + otherReWorkflowName + "工艺的属性", function (r) {
            if (r) {
                var formData = {
                    action: "GetStepTimeForOtherRev",
                    otherRevWorkflowId: otherRevWorkflowId,
                    workflowId: workflowId,
                    dtStep: JSON.stringify(datas)
                };
                $.postAjax({
                    url: page.mainUrl,
                    data: formData,
                    ajaxSuccess: function (res) {
                        if (res.Result) {
                            page.search(workflowId);
                        }
                        displayMessage2(res.Message, res.Result, ["btnOtherRev"]);
                    },
                    ajaxError: function () {
                        $('#btnOtherRev').linkbutton('enable'); //启用按钮
                    }
                });
            }
            $('#btnOtherRev').linkbutton('enable'); //启用按钮
        })
    },
    //保存
    save: function () {
        $('#btnSave').linkbutton('disable'); //禁用按钮
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取选中行
        if (datas.length === 0) {
            displayMessage2("工序表没有选择数据", false, ["btnSave"]);
            return;
        }
        for (var i = 0; i < datas.length; i++) {
            datas[i].MAKEEMPCOUNTS = datas[i].MAKEEMPCOUNTS + ""; //需将该字段转换为字符型
        }
        var workflowId = $("#workflowContent").workflowCombo("getValue");
        if (isNullOrWhiteSpaceStr(workflowId)) {
            displayMessage2("未选择工艺", false, ["btnSave"]);
            return;
        }
        var resDatas = [];
        var stepResData = $("#gdStepResource").datagrid('getRows'); //获取工序设备所有行
        for (var i = 0; i < stepResData.length; i++) {
            if (stepResData[i].SPECRESOURCEINFOID === "") { //如果工序设备ID为空，则添加该设备
                resDatas.push(stepResData[i]);
            }
        }
        var formData = {};
        formData.action = "SaveStepWorkCenterInfo";
        formData.workflowId = workflowId; //选择的工艺
        formData.dtStep = JSON.stringify(datas); //选择的工序（包含需要更新的工区、工时、生产周期信息、商品工时）
        formData.dtRes = JSON.stringify(resDatas); //需要添加的设备信息
        formData.deleResIds = resource.deleDataById; //需要删除的设备ID
        $.postAjax({
            url: page.mainUrl,
            data: formData,
            ajaxSuccess: function (res) { //ajax成功后
                if (res.Result) {
                    $("#gdStepInfo").datagrid('uncheckAll');
                    page.clearPage();
                }
                displayMessage2(res.Message, res.Result, ["btnSave"]);
            },
            ajaxError: function () {
                $("#btnSave").linkbutton('enable');
            }
        });
    },
    //url参数验证
    pageVaild: function (arr) {
        if (arr.length === 0) {
            displayMessage("未指定功能类型", false);
            return false;
        }
        var isvaild = true;
        var tarr = ["1", "2", "3", "4", "5"];
        for (var i = 0; i < arr.length; i++) {
            if ($.inArray(arr[i], tarr) === -1) {
                displayMessage("参数出错", false);
                isvaild = false;
                break;
            }
        }
        return isvaild;
    },
    //清空页面
    clearPage: function () {
        var typearr = page.pageType.split(',');
        for (var i = 0; i < typearr.length; i++) {
            switch (typearr[i]) {
                case "1":
                    $('#selTeam').combobox("setValue", "");
                    break;
                case "2":
                    $('#txtUnitTime').textbox('setValue', "");
                    $('#txtPrepareTime').textbox('setValue', "");
                    $('#txtRemark').textbox('setValue', "");
                    break;
                case "3":
                    resource.clearGrid();
                    break;
                case "4":
                    $('#txtPrepareCycle').textbox('setValue', '');
                    $('#txtMakeCycle').textbox('setValue', '');
                    $('#txtTransfeCycle').textbox('setValue', '');
                    $("#txtShipCycle").textbox("setValue", "");
                    $("#txtCommiCycle").textbox("setValue", "");
                    $("#txtMakeEmpCounts").textbox("setValue", "");
                    $("#txtChargingQty").textbox("setValue", "");
                    $("#txtRemark").textbox("setValue", "");
                    break;
                case "5":
                    $('#txtCommodityTime').textbox("setValue", "");
                    break;
                default:
                    break;
            }
        }
    },
    //工序表格事件
    stepGridEvent: function () {
        $("#gdStepInfo").datagrid({
            onCheck: function (index, rowData) {
                clearMessage();
                resource.bindStepResData();
            },
            onUncheck: function (index, rowData) {
                clearMessage();
                resource.bindStepResData();
            },
            onCheckAll: function (index, rowData) {
                clearMessage();
                resource.bindStepResData();
            },
            onUncheckAll: function (index, rowData) {
                clearMessage();
                resource.bindStepResData();
            }
        });
    },



    //实做工时传递
    WorkTimeUp: function (workflowId, specids) {
        var formData = {
            action: "GetSpecWorkTimeData",
            workflowId: workflowId,
            specids: specids.toString(), 
            productName: $("#productContent").productCombo("getText"),
            isSomeWorkflow: $("#isSomeWorkflow").prop('checked'),
            isThisFactory: false
        };

        $.postAjax({
            url: page.mainUrl,
            data: formData,
            ajaxSuccess: function (res) {
                //$.messager.progress('close');
                if (res.Result) {
                    displayMessage(res.Message, true, ["btnWorkTimeUp"]);
                    return;
                } else {
                    displayMessage(res.Message, res.Result);
                }
            }
        });
    }
};
//工区维护对象
var workCenter = {
    init: function () {
        workCenter.bind();
        workCenter.initData();
    },
    bind: function () {
        //        $("#gdStepInfo").datagrid("showColumn", "WORKCENTERNAME");
        //        $("#gdStepInfo").datagrid("showColumn", "TEAMNAME");
        //添加工区班组
        $("#btnAddWorkCenter").on("click", function () {
            clearMessage();
            workCenter.addWorkCenterData();
        });
        //清除工区班组
        $("#btnClearWorkCenter").on("click", function () {
            clearMessage();
            workCenter.clearWorkCenterData();
        });
    },
    //初始化数据
    initData: function () {
        $.postAjax({
            url: page.mainUrl,
            data: { action: "InitWorkCenterPage" },
            ajaxSuccess: function (res) { //ajax成功后
                if (!res.Result) {
                    displayMessage(res.Message, res.Result);
                    return;
                }
                //绑定工区
                $("#selWorkCenter").combobox({
                    valueField: 'WORKCENTERID',
                    textField: 'WORKCENTERNAME',
                    panelHeight: 200,
                    data: res.DBTable, //下拉数据
                    onSelect: function (record) {
                        workCenter.bindTeam(record.WORKCENTERID); //加载班组
                    }
                });
            }
        });
    },
    //绑定班组
    bindTeam: function (workcenterId) {
        $.postAjax({
            url: page.commonUrl,
            data: { action: "GetTeam", workCenterId: workcenterId },
            ajaxSuccess: function (res) { //ajax成功后
                if (!res.Result) {
                    displayMessage(res.Message, res.Result);
                    return;
                }
                //绑定班组
                $("#selTeam").combobox({
                    valueField: 'TEAMID',
                    textField: 'TEAMNAME',
                    panelHeight: 200,
                    data: res.DBTable //下拉数据
                });
            }
        });
    },
    //将工区班组信息添加到工序列表里
    addWorkCenterData: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择需要添加的工序", false);
            return;
        }
        var newsData = {};
        var workcenterid = $('#selWorkCenter').combobox('getValue');
        var workcentername = $('#selWorkCenter').combobox('getText');
        if (isNullOrWhiteSpaceStr(workcenterid)) {
            displayMessage("未选择工区", false);
            return;
        }
        newsData.WORKCENTERID = workcenterid;
        newsData.WORKCENTERNAME = workcentername;
        var teamid = $('#selTeam').combobox('getValue');
        var teamname = $('#selTeam').combobox('getText');
        if (isNullOrWhiteSpaceStr(teamid)) {
            teamname = "";
        }
        newsData.TEAMID = teamid;
        newsData.TEAMNAME = teamname;
        newsData.IsUpdateWorkcenter = "1"; //确定更新工区班组

        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newsData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    },
    //清除工序列表中的工序工区班组信息
    clearWorkCenterData: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择需要清除的工序", false);
            return;
        }
        var newsData = {};
        newsData.WORKCENTERID = "";
        newsData.WORKCENTERNAME = "";
        newsData.TEAMID = "";
        newsData.TEAMNAME = "";
        newsData.IsUpdateWorkcenter = "1"; //确定更新工区班组
        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newsData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    }
};
//工时维护对象
var stepTime = {
    init: function () {
        stepTime.bind();
    },
    bind: function () {
        $("#gdStepInfo").datagrid("showColumn", "DURATIONPERUNIT");
        $("#gdStepInfo").datagrid("showColumn", "SETUPTIME");
        $("#gdStepInfo").datagrid("showColumn", "NOTES");
        //添加工时信息
        $("#btnAddTime").on("click", function () {
            clearMessage();
            stepTime.addTimeData();
        });
        //清除工时信息
        $("#btnClearTime").on("click", function () {
            clearMessage();
            stepTime.clearTimeData();
        });
    },
    //将工时信息添加到工序列表里
    addTimeData: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择需要添加的工序", false);
            return;
        }
        var newsData = {};
        var unitTime = $('#txtUnitTime').textbox('getValue');
        if (isNullOrWhiteSpaceStr(unitTime)) {
            displayMessage("请填写单位工时", false);
            return;
        }
        newsData.DURATIONPERUNIT = unitTime;
        var prepareTime = $('#txtPrepareTime').textbox('getValue');
        if (isNullOrWhiteSpaceStr(prepareTime)) {
            displayMessage("请填写准备工时", false);
            return;
        }
        newsData.SETUPTIME = prepareTime;
        var notes = $('#txtRemark').textbox('getValue');
        if (isNullOrWhiteSpaceStr(notes) == false) {
            newsData.NOTES = notes;
        }
        newsData.IsUpdateTime = "1"; //确定更新工时

        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newsData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    },
    //清除工序列表中的工序工时信息
    clearTimeData: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择需要清除的工序", false);
            return;
        }
        var newsData = {};
        newsData.DURATIONPERUNIT = "";
        newsData.SETUPTIME = "";
        newsData.NOTES = "";
        newsData.IsUpdateTime = "1"; //确定更新工时
        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newsData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    }
};
//设备维护对象
var resource = {
    deleDataById: "", //需要删除的Id
    init: function () {
        resource.bind();
        resource.clearGrid();
    },
    bind: function () {
        //工序设备列表
        $("#gdStepResource").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'SPECNO', title: '工序号', width: 80 },
                 { field: 'OPDESC', title: '工序名', width: 160 },
                 { field: 'RESOURCENAME', title: '设备编码', width: 130 },
                 { field: 'DESCRIPTION', title: '设备名称', width: 150 },
                 { field: 'SPECID', hidden: true },
                 { field: 'SPECRESOURCEINFOID', hidden: true },
                 { field: 'RESOURCEID', hidden: true }
                ]]
        });
        //设备列表
        $("#gdResource").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'RESOURCENAME', title: '设备编码', width: 180 },
                 { field: 'DESCRIPTION', title: '设备名称', width: 170 },
                 { field: 'FACTORYNAME', title: '设备分厂', width: 100 },
                 { field: 'RESOURCEID', hidden: true }
                ]]
        });
        //添加设备
        $("#btnAddResource").on("click", function () {
            clearMessage();
            resource.add();
        });
        //移除设备
        $("#btnDeleResource").on("click", function () {
            clearMessage();
            resource.remove();
        });
        //搜索设备
        $("#btnResource").on("click", function () {
            clearMessage();
            resource.search();
        });
    },
    //绑定工序设备列表
    bindStepResData: function () {
        resource.clearGrid();
        var checkedData = $("#gdStepInfo").datagrid("getChecked");
        if (checkedData.length === 0) {
            return;
        }
        var specIds = "";
        for (var i = 0; i < checkedData.length; i++) {
            specIds += "'" + checkedData[i].SPECID + "',";
        }
        var formData = { action: "GetStepResourceDatas", specIds: specIds };
        $.postAjax({
            url: page.mainUrl,
            data: formData,
            ajaxSuccess: function (res) { //ajax成功后
                if (res.Result) {
                    $("#gdStepResource").datagrid('loadData', { rows: res.DBTable });
                } else {
                    displayMessage(res.Message, res.Result);
                }
            },
            ajaxError: function () {
                $("#btnSave").linkbutton('enable');
            }
        });
    },
    //搜索设备并绑定设备列表
    search: function () {
        clearEasyuiDataGrid("gdResource", 1);
        var resourceName = $("#txtResource").textbox("getValue");
        var resourceNum = $("#txtResourceNum").textbox("getValue");
        var formData = { action: "GetResourceDatas", resourceName: resourceName, resourceNum: resourceNum };
        $.postAjax({
            url: page.mainUrl,
            data: formData,
            ajaxSuccess: function (res) { //ajax成功后
                if (!res.Result) {
                    displayMessage(res.Message, res.Result);
                    return;
                }
                $("#gdResource").datagrid('loadData', { rows: res.DBTable });
            }
        });
    },
    //添加设备到工序
    add: function () {
        var stepData = $("#gdStepInfo").datagrid("getChecked");
        if (stepData.length === 0) {
            displayMessage("请选择需要添加的工序", false);
            return;
        }
        var resData = $("#gdResource").datagrid("getChecked");
        if (resData.length === 0) {
            displayMessage("请选择设备", false);
            return;
        }
        var newData = [];
        for (var i = 0; i < stepData.length; i++) {
            for (var j = 0; j < resData.length; j++) {
                if (resData[j].FACTORYID === stepData[i].SFACTORYID) { //如果设备和工序是同分厂的，则添加
                    newData.push($.extend({}, resData[j], { SPECNO: stepData[i].SPECNO, OPDESC: stepData[i].OPDESC, SPECID: stepData[i].SPECID, SPECRESOURCEINFOID: "" }));
                }
            }
        }
        if (newData.length === 0) {
            displayMessage("设备工序分厂不一致", false);
            return;
        }
        var isnew = true;
        var stepResData = $("#gdStepResource").datagrid("getRows"); //获取工序设备列表
        for (var i = 0; i < newData.length; i++) {
            isnew = true;
            for (var j = 0; j < stepResData.length; j++) {//对比添加的工序设备，同工序同设备跳过
                if (stepResData[j].SPECNO === newData[i].SPECNO && stepResData[j].RESOURCEID === newData[i].RESOURCEID) {
                    isnew = false;
                }
            }
            if (isnew) {
                $("#gdStepResource").datagrid("appendRow", newData[i]);
            }
        }
        $("#gdStepResource").datagrid("acceptChanges");
    },
    //移除工序设备列表
    remove: function () {
        var stepResData = $("#gdStepResource").datagrid("getChecked");
        if (stepResData.length === 0) {
            displayMessage("请选择设备工序", false);
            return;
        }
        for (var i = 0; i < stepResData.length; i++) {
            if (stepResData[i].SPECRESOURCEINFOID !== "") {
                resource.deleDataById += "'" + stepResData[i].SPECRESOURCEINFOID + "',";
            }
            var index = $("#gdStepResource").datagrid("getRowIndex", stepResData[i]);
            $("#gdStepResource").datagrid("deleteRow", index);
        }
        //        var formData = { action: "DeleteStepResource", stepResIds: stepResIds };
        //        $.postAjax({
        //            url: page.mainUrl,
        //            data: formData,
        //            ajaxSuccess: function (res) { //ajax成功后
        //                if (res.Result) {
        //                    for (var i = 0; i < stepResData.length; i++) {
        //                        var index = $("#gdStepResource").datagrid("getRowIndex", stepResData[i]);
        //                        $("#gdStepResource").datagrid("deleteRow", index);
        //                    }
        //                }
        //                displayMessage(res.Message, res.Result);
        //            }
        //        });

    },
    clearGrid: function () {
        clearEasyuiDataGrid("gdStepResource");
        clearEasyuiDataGrid("gdResource");
        resource.deleDataById = "";
    }
};
//生产周期维护对象
var productionCycle = {
    init: function () {
        productionCycle.bind();
    },
    bind: function () {
        $("#gdStepInfo").datagrid("showColumn", "PREPARECYCLE");
        $("#gdStepInfo").datagrid("showColumn", "MAKECYCLE");
        $("#gdStepInfo").datagrid("showColumn", "TRANSFERCYCLE");
        $("#gdStepInfo").datagrid("showColumn", "SHIPCYCLE");
        $("#gdStepInfo").datagrid("showColumn", "COMMICYCLE");
        $("#gdStepInfo").datagrid("showColumn", "MAKEEMPCOUNTS");
        $("#gdStepInfo").datagrid("showColumn", "CHARGINGQTY");
        $("#gdStepInfo").datagrid("showColumn", "MULTIMACHINEPARALLELNAME");
        $("#gdStepInfo").datagrid("showColumn", "NOTES");
        //添加生产周期信息
        $("#btnAddCycle").on("click", function () {
            clearMessage();
            productionCycle.addCycle();
        });
        //移除生产周期信息
        $("#btnClearCycle").on("click", function () {
            clearMessage();
            productionCycle.clearCycle();
        });
    },
    //添加生产周期
    addCycle: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择需要添加的工序", false);
            return;
        }
        var newCycData = {};
        var prepareCycle = $("#txtPrepareCycle").textbox("getValue");
        if (!window.isNaN(prepareCycle) && prepareCycle !== "") {
            newCycData.PREPARECYCLE = prepareCycle; //准备时间
        }
        var makeCycle = $("#txtMakeCycle").textbox("getValue");
        if (!window.isNaN(makeCycle) && makeCycle !== "") {
            newCycData.MAKECYCLE = makeCycle; //加工时间
        }
        var transfeCycle = $("#txtTransfeCycle").textbox("getValue");
        if (!window.isNaN(transfeCycle) && transfeCycle !== "") {
            newCycData.TRANSFERCYCLE = transfeCycle; //运转时间
        }

        var shipCycle = $("#txtShipCycle").textbox("getValue");
        if (!window.isNaN(shipCycle) && shipCycle !== "") {
            newCycData.SHIPCYCLE = shipCycle; //转工周期
        }
        var commiCycle = $("#txtCommiCycle").textbox("getValue");
        var res = productionCycle.vaildDayNumber(commiCycle);
        if (res.result) {
            newCycData.COMMICYCLE = commiCycle; //外委周期
        }
        if (res.msg !== "") {
            displayMessage("外委周期" + res.msg, false);
            return;
        }

        var makeEmpCounts = $("#txtMakeEmpCounts").textbox("getValue");
        if (!isNullOrWhiteSpaceStr(makeEmpCounts)) {
            newCycData.MAKEEMPCOUNTS = makeEmpCounts; //作业人数
        }
        var chargingQty = $("#txtChargingQty").textbox("getValue");
        if (!isNullOrWhiteSpaceStr(chargingQty)) {
            newCycData.CHARGINGQTY = chargingQty; //装炉/槽量
        }

        var multimachineParallelName = $("#selMultimachineParallel").combobox("getText");
        var multimachineParallel = $("#selMultimachineParallel").combobox("getValue");
        if (!isNullOrWhiteSpaceStr(multimachineParallel)) {
            newCycData.MULTIMACHINEPARALLEL = multimachineParallel; //多机并行
            newCycData.MULTIMACHINEPARALLELNAME = multimachineParallelName;
        }

        var notes = $("#txtRemark").textbox("getValue");
        if (!isNullOrWhiteSpaceStr(notes)) {
            newCycData.NOTES = notes; //备注
        }
        if (JSON.stringify(newCycData) === "{}") {
            displayMessage("没有填写需添加的数据", false);
            return;
        }
        newCycData.IsUpdateCycle = "1";
        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newCycData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    },
    //清除生产周期
    clearCycle: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择工序", false);
            return;
        }
        var newCycData = {
            PREPARECYCLE: "",
            MAKECYCLE: "",
            TRANSFERCYCLE: "",
            SHIPCYCLE: "",
            COMMICYCLE: "",
            MAKEEMPCOUNTS: "",
            CHARGINGQTY: "",
            NOTES: "",
            IsUpdateCycle: "1"
        };
        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newCycData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    },
    //验证天数
    vaildDayNumber: function (days) {
        var res = { result: true, msg: "" };
        if (isNullOrWhiteSpaceStr(days)) {
            res.result = false;
            res.msg = "";
            return res;
        }
        if (window.isNaN(days)) {
            res.result = false;
            res.msg = "必须为数字";
            return res;
        }
        var strDay = JSON.stringify(days);
        strDay = strDay.trim('"');
        var strDays = strDay.split('.');

        if (strDays.length > 2) {
            res.result = false;
            res.msg = "小数点位必须为1位";
        }
        if (strDays.length === 2) {
            if (strDays[1] !== "5") {
                res.result = false;
                res.msg = "小数点必须为0.5倍";
            }
        }
        return res;
    }
};
//热表商品工时维护
var rbTime = {
    init: function () {
        rbTime.bind();
        $("#divOtherRev").hide(); //隐藏其他版本
    },
    bind: function () {
        $("#gdStepInfo").datagrid("showColumn", "COMMODITYTIME");
        //添加商品工时
        $("#btnAddCommodityTime").on("click", function () {
            clearMessage();
            rbTime.addTime();
        });
        //移除商品工时
//        $("#btnClearCommodityTime").on("click", function () {
//            clearMessage();
//            rbTime.clearTime();
//        });
    },
    //添加商品工时
    addTime: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择需要添加的工序", false);
            return;
        }
        var newCycData = {};
        var commodityTime = $("#txtCommodityTime").textbox("getValue");
        if (!window.isNaN(commodityTime) && $.trim(commodityTime) !== "") {
            newCycData.COMMODITYTIME = commodityTime; //商品工时
            newCycData.IsUpdateCommodityTime = "1";
        }
        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newCycData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    },
    //清除商品工时（弃用）
    clearTime: function () {
        var datas = $("#gdStepInfo").datagrid('getChecked'); //获取所有勾选的行
        if (datas.length === 0) {
            displayMessage("未选择工序", false);
            return;
        }
        var newCycData = {
            COMMODITYTIME: "",
            IsUpdateCommodityTime: "1"
        };
        for (var i = 0; i < datas.length; i++) {
            var index = $("#gdStepInfo").datagrid('getRowIndex', datas[i]);
            var row = $.extend({}, datas[i], newCycData);
            $("#gdStepInfo").datagrid('updateRow', { index: index, row: row });
        }
        $("#gdStepInfo").datagrid('acceptChanges');
    }
};

page.init();

